<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4685 2010-08-16 08:54:12Z huuphuoc $
 * @since		2.0.7
 */
try{
?>
<?php
$this->headTitle()->prepend($this->translate('page_list_page_title') . ' - ');
$this->headScript()->appendFile($this->APP_STATIC_SERVER . '/js/jquery.json-2.3.min.js');
?>
<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/page.png'; ?>" alt=""/>
    <span><?php echo $this->translate('page_list_breadcrumb'); ?></span>
</div>
<div style="width: 100%;">
    <form id="frmActions" action="" method="post">
        <div id="actions">
            <input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('page_add_breadcrumb'); ?>" onclick="location = '<?php echo $this->url(array(), 'page_page_add'); ?>';"/>
            <input type="submit" id="btnDelete" class="button" value="<?php echo $this->translate('page_list_action_delete_selected'); ?>"/>
            <input type="hidden" id="hddIDs" value="" name="ids"/>
        </div>
        <table class="list" cellspacing="0">
            <thead>
                <tr>
                    <td width="5%">
                        <input type="checkbox" class="chkAll"/>
                    </td>
                    <td width="35%">
                        <?php echo $this->translate('page_name'); ?>
                    </td>
                    <td width="35%">
                        <?php echo $this->translate('page_slug'); ?>
                    </td>
                    <td width="10%">
                        <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                    </td>
                    <td width="15%">
                        <?php echo $this->translate('page_action'); ?>
                    </td>
                </tr>
            </thead>
            <tbody>
                <?php
                        $count = count($this->pages);
                        if ($count == 0) {
                ?>
                            <tr>
                                <td colspan="4" style="color: #fc0202;">
                        <?php echo $this->translate('page_list_not_found'); ?>
                        </td>
                    </tr>
                <?php
                        } else {
                            $count = count($this->pages);
                            $rightStack = array();
                            for ($i = 0; $i < $count; $i++) {
                                while (count($rightStack) > 0 && $rightStack[count($rightStack) - 1] < $this->pages[$i]->rht) {
                                    array_pop($rightStack);
                                }
                ?>
                                <tr id="page-<?php echo $this->pages[$i]->id ?>" <?php if ($i == $count - 1)echo 'class="last"'; ?>>
                                <td>
                                    <input type="checkbox" class="chkId" value="<?php echo $this->pages[$i]->id; ?>"/>
                                </td>
                                <td><?php echo str_repeat('--', count($rightStack)) . $this->pages[$i]->name; ?></td>
                                <td><?php echo $this->pages[$i]->slug; ?></td>
                                <td>
                        <?php
                                if (($translations = $this->translationItems($this->pages[$i])) != null) {
                                    foreach ($translations as $language => $translation) {
                                        if ($translation == null && $language != $this->pages[$i]->language) {
                                            if (isset($translations[$this->APP_DEFAULT_LANG])) {
                                            
                        ?>
                                                <a class="a_helper_action_add" href="<?php echo $this->url(array('source_id' => $translations[$this->APP_DEFAULT_LANG]->id), 'page_page_add_localization'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                        <?php } else {?>
                                                <a class="a_helper_action_add" href="<?php echo $this->url(array(), 'page_page_add'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                        <?php }
                                        } elseif (($translation == null && $language == $this->pages[$i]->language) || ($translation != null && $translation->id == $this->pages[$i]->id)) { ?>
                                            <a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $this->pages[$i]->id), 'page_page_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                        <?php } elseif ($translation != null && $translation->id != $this->pages[$i]->id) {?>
                                            <a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $translation->id), 'page_page_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                        <?php
                                        }
                                    }
                                }
                        ?>
                            </td>
                            <td>
                        <?php if ($this->allow('edit')) {?>
                                    <a href="<?php echo $this->url(array('id' => $this->pages[$i]->id), 'page_page_edit'); ?>" title=""><?php echo $this->translate('page_list_action_edit'); ?></a>
                        <?php } ?>
                        <?php if ($this->allow('delete')) { ?>
                                    | <a class="deleteAction" rel="<?php echo $this->pages[$i]->id ?>" href="#" title=""><?php echo $this->translate('page_list_action_delete'); ?></a>
                        <?php } ?>
                                    </td>
                                </tr>
                        <?php
                                $rightStack[] = $this->pages[$i]->rht;
                            }
                        }
                    ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td >
                            <input type="checkbox" class="chkAll"/>
                        </td>
                        <td >
                            <?php echo $this->translate('page_name'); ?>
                        </td>
                        <td >
                            <?php echo $this->translate('page_slug'); ?>
                        </td>
                        <td >
                            <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                        </td>
                        <td>
                            <?php echo $this->translate('page_action'); ?>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </form>
</div>



<?php //if ($this->allow('order')) : ?>
<!--                            <div class="grid_12 t_a_top t_a_bottom">-->
<!--                                <button id="saveButton"><span><?php //echo $this->translator('page_list_save_button'); ?></span></button>
                            </div>-->
<?php //endif; ?>

<script type="text/javascript">
$(document).ready(function() {

    <?php
    if ($this->allow('delete')) : ?>
        $(".deleteAction").click(function(){
        if(confirm("<?php echo $this->translate('page_list_action_delete_confirm')?>")){
            var id = $(this).attr("rel");
            $.ajax({
                type: 'POST',
                url: "<?php echo $this->url(array(), 'page_page_delete'); ?>",
                data: { id: id },
                success: function(response) {
                    if ('RESULT_OK' == response) {
                        $('#page-' + id).fadeOut('slow');
                    }
                }
            });
        }
        return false;
      });
      $("#btnDelete").click(function(){
		var arrChkId = $(".chkId");
		var ids = new Array();
		for(var i =0; i < arrChkId.length; i++){
			if(arrChkId.eq(i).attr("checked")){
                        ids.push(arrChkId.eq(i).val());
			}
		}
                if(ids.length > 0){
			if(confirm("<?php echo $this->translate('page_list_action_delete_many_confirm') ?>")){
                            $.ajax({
                                type: 'POST',
                                url: "<?php echo $this->url(array(), 'page_page_delete'); ?>",
                                data: { id: $.toJSON(ids) },
                                success: function(response) {
                                    if ('RESULT_OK' == response) {
                                        for(var i in ids)
                                            $('#page-' + ids[i]).fadeOut('slow');
                                    }
                                }
                            });
			}else{
				return false;
			}
		}else{
			alert("<?php echo $this->translate('page_list_bulk_action_none_selected')?>");
			return false;
		}
            return false;
	});

        
<?php endif; ?>
});
</script>
<?php } catch(Exception $ex){ echo $ex->getMessage();}?>